package com.ruoyi.aftserv.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aftserv.domain.AfterServiceApply;
import com.ruoyi.aftserv.domain.AfterServiceApplyInfo;
import com.ruoyi.aftserv.domain.AfterServiceApplyInfoQuery;
import com.ruoyi.aftserv.service.IAfterServiceApplyInfoService;
import com.ruoyi.aftserv.service.IAfterServiceApplyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.ManageStudent;
import com.ruoyi.oa.service.IManageStudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 微信课后服务Controller
 * 
 * @author 张艳羽
 * @date 2024-08-21
 */
@RestController
@RequestMapping("/wxapi/aftserv")
public class WxAfterServiceController extends BaseController
{
    @Autowired
    private IAfterServiceApplyService afterServiceApplyService;
    @Autowired
    private IAfterServiceApplyInfoService afterServiceApplyInfoService;
    @Autowired
    private IManageStudentService studentService;

    /**
     * 查询报名方案管理列表
     */
    @GetMapping("/listApply")
    public AjaxResult listApply(AfterServiceApply afterServiceApply)
    {
        afterServiceApply = new AfterServiceApply();
        afterServiceApply.setIsApplying("1");
        List<AfterServiceApply> list = afterServiceApplyService.selectAfterServiceApplyList(afterServiceApply);
        return AjaxResult.success(list);
    }
    @GetMapping("/listApplyInfo")
    public AjaxResult listApplyInfo(Integer applyId, Integer stuId)
    {
        Long applyIdLong = null != applyId ? Long.valueOf(applyId) : null;
        Long stuIdLong = null != stuId ? Long.valueOf(stuId) : null;
        AfterServiceApplyInfoQuery info = new AfterServiceApplyInfoQuery(applyIdLong, stuIdLong);
        List<AfterServiceApplyInfoQuery> list = afterServiceApplyInfoService.selectAfterServiceApplyInfoQueryList(info);
        return AjaxResult.success(list);
    }
    /**
     * 查询学生
     */
    @GetMapping("/getStuByStuno")
    public AjaxResult listStudent(String stuno)
    {
        if(StringUtils.isEmpty(stuno)) {
            return AjaxResult.error("学号不能为空！");
        }
        String status = "1";
        ManageStudent student = studentService.getStudentByStuno(stuno, status);
        return AjaxResult.success(student);
    }

    /**
     * 新增课后服务报名
     */
    @PostMapping("/addApply")
    @ResponseBody
    public AjaxResult addApply(@RequestBody JSONObject object)
    {
        Integer applyIdInt = (Integer)object.get("applyId");
        Integer stuIdInt = (Integer) object.get("stuId");
        String stuno = (String) object.get("stuno");
        if(null == applyIdInt || null == stuIdInt || StringUtils.isEmpty(stuno)) {
            return AjaxResult.error("请检查参数！");
        } else {
            Long applyId = Long.valueOf(applyIdInt);
            Long stuId = Long.valueOf(stuIdInt);
            AfterServiceApplyInfo info = new AfterServiceApplyInfo(applyId, stuId);
            //TODO 检查是否已经报过名了
            List<AfterServiceApplyInfo> infos = this.afterServiceApplyInfoService.selectAfterServiceApplyInfoList(info);
            if(infos.size() > 0) {
                return AjaxResult.error("请勿重复报名！");
            }

            return toAjax(afterServiceApplyInfoService.insertAfterServiceApplyInfo(info));
        }

    }


}
